package com.podium.web.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.podium.web.system.entity.SysUserRole;
import com.podium.web.system.mapper.SysUserRoleMapper;
import com.podium.web.system.service.SysUserRoleService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 邵文泽
 * @description 用户角色服务实现类
 * @date 2025/4/30 11:58
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

    @Override
    public List<SysUserRole> list(SysUserRole sysUserRole) {
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(sysUserRole.getUserId() != null, SysUserRole::getUserId, sysUserRole.getUserId())
                .eq(sysUserRole.getRoleId() != null, SysUserRole::getRoleId, sysUserRole.getRoleId())
        ;
        return super.list(queryWrapper);
    }

    @Override
    public boolean editUserRole(SysUserRole sysUserRole) {
        List<SysUserRole> list = new ArrayList<>();
        for (Long roleId : sysUserRole.getRoleIds()) {
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(sysUserRole.getUserId());
            userRole.setRoleId(roleId);
            list.add(userRole);
        }
        return super.saveBatch(list);
    }

    @Override
    public void removeByUserId(Long userId) {
        super.remove(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, userId));
    }
}
